package com.mybatils.utils;

import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/5/21 14:16
 * @since JDK 1.8
 */
public class SqlUtils {


    public static String selectCreateOne(String table,String field,String value){
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("select * from ");
        stringBuffer.append(table);
        stringBuffer.append(" where ").append(field).append(" ='").append(value)
                .append("'");
        return stringBuffer.toString();
    }


    /**
     *
     * @param tableName  表明
     * @param dataMap   要生成insert sql 的map
     * @param fieldReplace  要替换字段名称的map
     * @return
     */
    public static String insertSqlCreate(String tableName,Map<String, Object> dataMap,Map<String, String> fieldReplace){
        if(dataMap==null) {
            return null;
        }
        StringJoiner sbField = new StringJoiner(",","(",")");
        //生成VALUES('value1','value2') 部分
        StringJoiner sbValue = new StringJoiner(",","(",")");
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("INSERT INTO ").append(tableName.toLowerCase());
        for(Map.Entry<String, Object> entry : dataMap.entrySet()){
            String mapKey = entry.getKey();
            if(entry.getValue()==null || "".equals(String.valueOf(entry.getValue()))){
                continue;
            }
            String mapValue =String.valueOf( entry.getValue());
            if(fieldReplace!=null&&fieldReplace.containsKey(mapKey)){
                String key = fieldReplace.get(mapKey)==null?mapKey:fieldReplace.get(mapKey);
                sbField.add("`" + key + "`");
                sbValue.add("'" + mapValue + "'");
                continue;
            }
            sbField.add("`" + mapKey + "`");
            sbValue.add("'" + mapValue + "'");
        }
        return stringBuffer.append(sbField.toString()).append(" VALUES ").append(sbValue.toString()).toString();
    }


    public static String inserSqlCreateOne(Map<String, Object> objectMap,String tableName){
        if(objectMap==null) {
            return null;
        }
        //生成INSERT INTO table(field1,field2) 部分
        StringJoiner sbField = new StringJoiner(",","(",")");
        //生成VALUES('value1','value2') 部分
        StringJoiner sbValue = new StringJoiner(",","(",")");
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("INSERT INTO ").append(tableName.toLowerCase());
        for(Map.Entry<String, Object> entry : objectMap.entrySet()){
            String mapKey = entry.getKey();
            String mapValue =String.valueOf( entry.getValue());
            sbField.add("`" + mapKey + "`");
            sbValue.add("'" + mapValue + "'");
        }
        return stringBuffer.append(sbField.toString()).append(" VALUES ").append(sbValue.toString()).toString();
    }


    /**
     *
     * @param tableName 表名
     * @param dataMap  要更改的数据
     * @param field   更据哪一个属性的值来修改
     * @return
     */
    public static String updateSqlCreate(String tableName, Map<String, Object> dataMap,String field) {
        if(dataMap==null || ! dataMap.containsKey(field)) {
            return null;
        }
        String idValue =String.valueOf(dataMap.get(field)) ;
        StringBuffer sb = new StringBuffer();
        StringJoiner stringJoiner=new StringJoiner(",");
        sb.append("UPDATE "+ tableName.toLowerCase() +" SET ");
        for(Map.Entry<String, Object> entry : dataMap.entrySet()){
            if(entry.getValue()==null){
                continue;
            }
            String mapKey = entry.getKey();
            String mapValue = String.valueOf(entry.getValue());
            if(entry.getKey().toLowerCase().equals(field)) {
                continue;
            }
            stringJoiner.add("`" + mapKey + "`='" + mapValue + "' ");
        }
        sb.append(stringJoiner.toString()).append( " where ").append(field)
                .append(" =").append("'").append(idValue).append("'");
        return  sb.toString();
    }


}
